Importing your own data

It is possible to import your own rasters into a SimpleSDMLayer object. This requires defining a new type and two "helper" functions, which might seem a little bit convoluted, but helps immensely underneath in case you want to also download rasters from the web with different arguments. In this example, we will look at a data file produced by the OmniScape package, and which represents landscape connectivity in the Laurentians region of Québec. This example will also show how we can use the broadcast operation to modify the values of a raster.

using SimpleSDMLayers
using Plots
using StatsBase

The file comes with the package itself, so we can read it directly - this is a geotiff file, where values are floating point numbers representing connectivity.

file = joinpath(dirname(pathof(SimpleSDMLayers)), "..", "data", "connectivity.tiff")
"/home/runner/work/SimpleSDMLayers.jl/SimpleSDMLayers.jl/src/../data/connectivity.tiff"

To import this file as a SimpleSDMLayer, we need to create a type (MyConnectivityMap), and declare a method for latitudes and longitudes for this type, where the output is the range of latitudes and longitudes. This might seem cumbersome, but remember: it can be automated, and if you do not declare a latitude and longitude method, it will be assumed that the raster covers the entire globe. From a end-user perspective, it also removes the need to pass the bounding box of your layer as an argument, and to focus instead of the region of interest.

struct MyConnectivityMap <: SimpleSDMLayers.SimpleSDMSource end
SimpleSDMLayers.latitudes(::Type{MyConnectivityMap}) = (45.34523, 47.38457)
SimpleSDMLayers.longitudes(::Type{MyConnectivityMap}) = (-75.17734,-72.36486)

Now that this is done, we can read this file as a SimpleSDMResponse using the raster function:

mp = SimpleSDMLayers.raster(SimpleSDMResponse, MyConnectivityMap(), file)
SDM response → 1255×1205 grid with 863533 Float64-valued cells
Latitudes	(45.34604248605578, 47.38375751394422)
Longitudes	(-75.17617396351575, -72.36835810945273)

Because this file has raw values, which are not necessarily great for plotting, we will transform it to quantiles, using the StatsBase.ecdf function.

qfunc = ecdf(convert(Vector{Float64}, filter(!isnothing, mp.grid)))
StatsBase.ECDF{Array{Float64,1},StatsBase.Weights{Float64,Float64,Array{Float64,1}}}([3.0854051347115456, 3.190929431709246, 3.362648233544386, 3.5027200169879036, 3.9279171944297797, 4.154775752776106, 4.221778427534736, 4.225724864943838, 4.7993850092593755, 4.985787278928306  …  10880.545366221195, 10958.506426511878, 11005.89548389703, 11060.367688084245, 11089.36357573883, 11095.122131310669, 11122.504513226577, 11124.46445304352, 12209.352638285704, 13445.28877308455], Float64[])

And we can now broadcast this function to the layer:

qmap = broadcast(qfunc, mp)
SDM response → 1255×1205 grid with 863533 Float64-valued cells
Latitudes	(45.34604248605578, 47.38375751394422)
Longitudes	(-75.17617396351575, -72.36835810945273)

Finally, we are ready for plotting:

plot(qmap, frame=:grid, c=:cork, clim=(0,1))